package com.rui.study.commons.study.http;

import com.rui.study.commons.study.http.AbstractConcurrentRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

public class PDRIConcurrentRequest extends AbstractConcurrentRequest {

    private static final Logger LOGGER = LoggerFactory.getLogger(PDRIConcurrentRequest.class);

    public PDRIConcurrentRequest() {
    }

    public PDRIConcurrentRequest(int threadCount) {
        super(threadCount);
    }

    public PDRIConcurrentRequest(RestTemplate restTemplate, int threadCount) {
        super(restTemplate, threadCount);
    }

    @Override
    protected RequestEntity buildRequestEntity() {
        return null;
    }

    @Override
    protected void processAggregationResult(ResponseEntity<?> result, String threadName, AbstractConcurrentRequest.AggregationResult aggregationResult) {
        HttpStatus status = result.getStatusCode();
        StringBuilder message = new StringBuilder(threadName);
        message.append(" : ");
        message.append(status.value());

        Object body = result.getBody();
        if (body instanceof String && ((String) body).contains("未登录")) {
            message.append(" : ");
            message.append(body);

            Object obj = aggregationResult.getAttribute("count");
            long count;
            if (obj == null) {
                count = 1;
            } else {
                count = (Long) obj;
                count++;
            }
            aggregationResult.setAttribute("count", count);
        }
        LOGGER.info(message.toString());
    }

//    /**
//     * 执行器方法
//     * @param proxy
//     * @param method
//     * @param args
//     * @return
//     * @throws Throwable
//     */
//    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//        long start = System.currentTimeMillis();
//        LOGGER.info("Begin {}...", method.getName());
//        Object result = method.invoke(targetInstance, args);
//        LOGGER.info("Finish {}...", method.getName());
//        long end = System.currentTimeMillis();
//        LOGGER.info("Duration time: {}s", (end - start)/1000D);
//        return result;
//    }


    public static void test1() {
        AbstractConcurrentRequest concurrentRequest = new PDRIConcurrentRequest();
//        String catalog = "langtypes";
//        String url = String.format("http://pcms.pdri.d:8070/pdri/pcms/website/v102/index/kv/%s/ZH_CN", catalog);

//        String url = "http://study.rui.pub:8090/usermanager/admin/load?module=user&state=all";

        String url = "http://pcms.pdri.d:8070/pdri/pcms/publisher/v101/publisher/validcode/ZH_CN/777@888.999";

        try {
            URI uri = new URI(url);

            RequestEntity requestEntity = new RequestEntity(null, null, HttpMethod.GET, uri);
            AggregationResult resultList = concurrentRequest.concurrencyExchange(requestEntity, String.class);

        } catch (URISyntaxException e) {
            LOGGER.info("URI异常! Caused by: {}", e.getMessage());
        } catch (Exception e) {
            LOGGER.error("Internal Exception: {}", e.getMessage());
        }
        LOGGER.info("end");
    }

//    @Override
//    protected void testUriVariables() {
//        String url = "http://study.rui.pub:8090/usermanager/admin/load?module=order&form=ONE";
//        HttpEntity httpEntity = new HttpEntity<Object>(clientAuthenticationInfo());
//
//        String result;
//        try {
//            ResponseEntity<String> responseEntity = exchange(url, HttpMethod.GET, httpEntity, String.class, "user", "ALL");
//            result = responseEntity.getBody();
//            LOGGER.info("Result: {}", result);
//        } catch (Exception e) {
//            LOGGER.info("Catch exception: caused by {}", e.getMessage());
//        }
//    }
//
//    private HttpHeaders clientAuthenticationInfo() {
//        HttpHeaders headers = new HttpHeaders();
//        headers.add("Access-Token", "0123456789");
//        return headers;
//    }
//
//
//    public static void test2() {
//        ConcurrentRequest concurrentRequest = getInstance();
//        concurrentRequest.testUriVariables();
//    }

    public static void main(String[] args) {
        test1();
//        test2();
    }

}
